package me.zd.tool.hardlink.view.i18n;

import me.zd.tool.hardlink.service.I18nService;
import me.zd.tool.hardlink.view.I18nAdapter;

import javax.swing.*;
import java.util.HashMap;
import java.util.Map;

class I18nTabbedPaneAdapter implements I18nAdapter {
    private final JTabbedPane component;
    private final Map<Integer, String[]> i18nKeyMap;

    public I18nTabbedPaneAdapter(JTabbedPane component) {
        this.component = component;
        i18nKeyMap = new HashMap<>();
        for (int i = 0; i < component.getTabCount(); i++) {
            String[] keys = new String[2];
            keys[0] = component.getTitleAt(i);
            keys[1] = component.getToolTipTextAt(i);
            if (keys[0] != null || keys[1] != null) {
                i18nKeyMap.put(i, keys);
            }
        }
    }

    @Override
    public void updateI18n() {
        i18nKeyMap.forEach((index, keys) -> {
            if (keys[0] != null) {
                component.setTitleAt(index, I18nService.INS.get(keys[0]));
            }
            if (keys[1] != null) {
                component.setToolTipTextAt(index, I18nService.INS.get(keys[1]));
            }
        });
    }

    @Override
    public void updateI18n(int index) {
        updateI18n();
    }
}
